-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: create pdf parser with unstructured #8
base: main
Are you sure you want to change the base?
Conversation
import re | ||
import pathlib | ||
|
||
pdf_path = r"C:\Users\ADMIN\Desktop\KTHAIS\twiga-warehouse\data\parsed\text.md" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The path should be relative inside the project, so that every developer has its own absolute path
("#####", "Header 4"), # Bold + italic | ||
] | ||
|
||
def preprocess_md(md_doc): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you type the input and the output? Example:
def foo(im_a_number: int) -> int:
return im_a_number + 1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same for other functions
chunk_size = 250 | ||
chunk_overlap = 30 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it would be interesting to declare these two variables as input of the functions so that we could play with them if needed. Example:
def recursive_split(md_header_splits, chunk_size: int = 250, chunk_overlap: int = 30):
"""Split document recursively."""
...
md_doc = preprocess_md(md_doc) | ||
|
||
# Split data | ||
md_header_splits = md_split(md_doc) | ||
#character_splits = recursive_split(md_header_splits) | ||
|
||
# Append metadata | ||
splits_data = [] | ||
for split in md_header_splits: | ||
splits_data.append({ | ||
"content": split.page_content, | ||
"metadata": split.metadata | ||
}) | ||
|
||
# Save JSON output | ||
output_dir = pathlib.Path(r"C:\Users\ADMIN\Desktop\KTHAIS\twiga-warehouse\data\parsed") | ||
output_dir.mkdir(exist_ok=True) | ||
|
||
output_json_path = output_dir / "text.json" | ||
with open(output_json_path, "w", encoding="utf-8") as json_file: | ||
json.dump(splits_data, json_file, ensure_ascii=False, indent=4) | ||
|
||
print(f"Markdown splits saved to {output_json_path}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move this inside a main
function? This also implies creating the if __name__ == "__main__":
famous line at the bottom of the file. Same for the other files
Fixes #4 : Experimented with unstructuredloader for pdf parsing. Has element_id, parent_id and document type metadata which is very useful. Coordinate metadata could be used to find images. Can (somewhat) handle math formulas which may be relevant if/when we expand to other subjects.